home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / xinfo101.lha / xinfo-1.01.01 / Info.c < prev    next >
C/C++ Source or Header  |  1991-01-09  |  65KB  |  2,430 lines

  1. #ifndef lint
  2. static char *rcsid = "$Header: /usr3/xinfo/RCS/Info.c,v 1.9 91/01/07 14:01:06 jkh Exp Locker: jkh $";
  3. #endif
  4.  
  5. #include "InfoP.h"
  6.  
  7. #include <X11/Shell.h>
  8. #include <X11/StringDefs.h>
  9. #include <X11/Xaw/AsciiText.h>
  10. #include <X11/Xaw/Box.h>
  11. #include <X11/Xaw/Command.h>
  12. #include <X11/Xaw/Dialog.h>
  13. #include <X11/Xaw/Label.h>
  14. #include <X11/Xaw/List.h>
  15. #include <X11/Xaw/Paned.h>
  16. #include <X11/Xaw/Viewport.h>
  17.  
  18. #include <sys/stat.h>
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include <pwd.h>
  22.  
  23. /*
  24.  *
  25.  *                   Copyright 1989, 1990
  26.  *                    Jordan K. Hubbard
  27.  *
  28.  *                PCS Computer Systeme, GmbH.
  29.  *                   Munich, West Germany
  30.  *
  31.  *
  32.  * This file is part of GNU Info widget.
  33.  *
  34.  * The GNU Info widget is free software; you can redistribute it and/or
  35.  * modify it under the terms of the GNU General Public License as published
  36.  * by the Free Software Foundation; either version 1, or (at your option)
  37.  * any later version.
  38.  *
  39.  * This software is distributed in the hope that it will be useful,
  40.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  42.  * GNU General Public License for more details.
  43.  *
  44.  * You should have received a copy of the GNU General Public License
  45.  * along with this software; see the file COPYING.  If not, write to
  46.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  47.  *
  48.  *
  49.  */
  50.  
  51. /*
  52.  * $Log:    Info.c,v $
  53.  * Revision 1.9  91/01/07  14:01:06  jkh
  54.  * Now handles compressed files, resource structure now properly declared
  55.  * (XtRInt -> XtRImmediate where appropriate). Node name saved for goto
  56.  * attempts.
  57.  * 
  58.  * Revision 1.8  90/11/12  18:06:46  jkh
  59.  * Removed aggregate initializations. GCC likes them, noone else does.
  60.  * 
  61.  * Revision 1.7  90/11/12  13:46:11  jkh
  62.  * Fixed bug with bell_volume resource
  63.  * 
  64.  * Revision 1.6  90/11/11  23:22:59  jkh
  65.  * Last minute fixes.
  66.  * 
  67.  * Revision 1.5  90/11/11  22:24:05  jkh
  68.  * Added option to enable/disable retention of arg text.
  69.  * 
  70.  * Revision 1.4  90/11/11  21:19:39  jkh
  71.  * Release 1.01
  72.  * 
  73.  * Revision 1.3  90/11/07  01:28:30  jkh
  74.  * Tweaked dialog popup to accept <return> as fast confirm.
  75.  * 
  76.  * Revision 1.2  90/11/06  15:12:47  jkh
  77.  * Fixed memory leaks
  78.  * 
  79.  * Revision 1.1  90/11/06  01:47:28  jkh
  80.  * Initial revision
  81.  * 
  82.  */
  83.  
  84. #define offset(name)    XtOffset(InfoWidget, info.name)
  85. #define CDT(name)    (XtPointer)(name)
  86.  
  87. Local XtResource resources[] = {
  88.      { XpNinfoPath, XpCInfoPath, XtRString, sizeof(String),
  89.         offset(path), XtRString, CDT(XpDefaultInfoPath)        },
  90.      { XpNinfoFile, XpCInfoFile, XtRString, sizeof(String),
  91.         offset(file), XtRString, CDT(XpDefaultInfoFile)        },
  92.      { XpNinfoNode, XpCInfoNode, XtRString, sizeof(String),
  93.         offset(node), XtRString, CDT(XpDefaultInfoNode)        },
  94.      { XpNbellVolume, XpCBellVolume, XtRInt, sizeof(int),
  95.         offset(bell_volume), XtRImmediate, CDT(XpDefaultBellVolume)    },
  96.      { XpNretainArg, XpCRetainArg, XtRBoolean, sizeof(Boolean),
  97.         offset(retain_arg), XtRImmediate, CDT(False)        },
  98.      { XpNprintCommand, XpCPrintCommand, XtRString, sizeof(String),
  99.         offset(printCmd), XtRString, CDT(XpDefaultPrintCommand)    },
  100.      { XtNcallback, XtCCallback, XtRCallback, sizeof(caddr_t),
  101.         offset(callback), XtRCallback, CDT(NULL)            },
  102. };
  103.  
  104. #undef offset
  105. #undef CDT
  106.  
  107. Local Boolean SetValues();
  108. Local XtGeometryResult GeometryManager();
  109. Local void Destroy();
  110. Local void Initialize();
  111. Local void Realize();
  112. Local void Resize();
  113.  
  114. /* Routines called directly by actions */
  115. Local void Abort();
  116. Local void ButtonSelection();
  117. Local void Confirm();
  118. Local void NodeDir();
  119. Local void NodeGoto();
  120. Local void NodeHelp();
  121. Local void NodeLast();
  122. Local void NodeMenuSelectByNumber();
  123. Local void NodeNext();
  124. Local void NodePrev();
  125. Local void NodePrint();
  126. Local void NodeQuit();
  127. Local void NodeSearch();
  128. Local void NodeTop();
  129. Local void NodeTutorial();
  130. Local void NodeUp();
  131. Local void NodeXRef();
  132.  
  133. /* Routines called directly from callbacks or indirectly by actions */
  134. Local void do_dialog_abort();
  135. Local void do_dialog_confirm();
  136. Local void do_goto();
  137. Local void do_menu();
  138. Local void do_menu_sel();
  139. Local void do_next();
  140. Local void do_popdown();
  141. Local void do_prev();
  142. Local void do_quit();
  143. Local void do_search();
  144. Local void do_up();
  145. Local void do_xref();
  146. Local void do_xref_sel();
  147.  
  148. /* Utility routines */
  149. Local Boolean getNode();
  150. Local Boolean parseTags();
  151. Local InfoWidget find_top();
  152. Local NodeInfo *popNode();
  153. Local NodeInfo *pushNode();
  154. Local String downcase();
  155. Local String eat_whitespace();
  156. Local String file_name();
  157. Local String find_file();
  158. Local String getFile();
  159. Local String get_arg();
  160. Local String normalize_whitespace();
  161. Local String offsetToString();
  162. Local String reverse();
  163. Local String search();
  164. Local String search_back();
  165. Local String strconcat();
  166. Local String substr();
  167. Local String trueName();
  168. Local int findNode();
  169. Local int iindex();
  170. Local int strcomp();
  171. Local int strncomp();
  172. Local void clear_arg();
  173. Local void dialog();
  174. Local void displayHeader();
  175. Local void displayNode();
  176. Local void feep();
  177. Local void getXY();
  178. Local void message();
  179. Local void parseHeader();
  180. Local void parseIndirect();
  181. Local void parseMenu();
  182. Local void parseNode();
  183. Local void parseXRefs();
  184. Local void showStatus();
  185. Local void strccpy();
  186.  
  187. Local XtActionsRec actionTable[] =
  188. {
  189.      {    "abort",        Abort            },
  190.      {    "confirm",        Confirm            },
  191.      {    "info_click",        ButtonSelection        },
  192.      {    "info_dir",        NodeDir            },
  193.      {    "info_goto",        NodeGoto        },
  194.      {    "info_last",        NodeLast        },
  195.      {    "info_menusel",        NodeMenuSelectByNumber    },
  196.      {    "info_next",        NodeNext        },
  197.      {    "info_nodeSearch",    NodeSearch        },
  198.      {    "info_popupHelp",    NodeHelp        },
  199.      {    "info_prev",        NodePrev        },
  200.      {    "info_print",        NodePrint        },
  201.      {    "info_quit",        NodeQuit        },
  202.      {    "info_top",        NodeTop            },
  203.      {    "info_tutorial",    NodeTutorial        },
  204.      {    "info_up",        NodeUp            },
  205.      {    "info_xref",        NodeXRef        },
  206.      {    NULL,            NULL            }
  207. };
  208.  
  209. Export InfoClassRec infoClassRec = {
  210.      {    /* core fields */
  211.     /* superclass        */    (WidgetClass)&compositeClassRec,
  212.     /* class_name        */    "Info",
  213.     /* widget_size        */    sizeof(InfoRec),
  214.     /* class_initialize    */    NULL,
  215.     /* class_part_initialize*/    NULL,
  216.     /* class_inited        */    FALSE,
  217.     /* initialize        */    Initialize,
  218.     /* initialize_hook    */    NULL,
  219.     /* realize        */    Realize,
  220.     /* actions        */    actionTable,
  221.     /* num_actions        */    XtNumber(actionTable),
  222.     /* resources        */    resources,
  223.     /* num_resources    */    XtNumber(resources),
  224.     /* xrm_class        */    NULLQUARK,
  225.     /* compress_motion    */    TRUE,
  226.     /* compress_exposure    */    TRUE,
  227.     /* compress_enterleave    */    TRUE,
  228.     /* visible_interest    */    FALSE,
  229.     /* destroy        */    Destroy,
  230.     /* resize        */    Resize,
  231.     /* expose        */    XtInheritExpose,
  232.     /* set_values        */    SetValues,
  233.     /* set_values_hook    */    NULL,
  234.     /* set_values_almost    */    XtInheritSetValuesAlmost,
  235.     /* get_values_hook    */    NULL,
  236.     /* accept_focus        */    XtInheritAcceptFocus,
  237.     /* version        */    XtVersion,
  238.     /* callback_private    */    NULL,
  239.     /* tm_table        */    NULL,
  240.     /* query_geometry    */    XtInheritQueryGeometry,
  241.     /* display_accelerator    */    XtInheritDisplayAccelerator,
  242.     /* extension        */    NULL
  243.      },
  244.      {    /* composite fields    */
  245.     /* geometry_manager    */    GeometryManager,
  246.     /* change_managed    */    NULL,
  247.     /* insert_child        */    XtInheritInsertChild,
  248.     /* delete_child        */    XtInheritDeleteChild,
  249.     /* extension        */    NULL,
  250.      },
  251.      { /* info fields        */
  252.     /* empty        */    0
  253.      }
  254. };
  255.  
  256. Export WidgetClass infoWidgetClass = (WidgetClass)&infoClassRec;
  257.  
  258. #ifndef tolower
  259. #include <X11/Xos.h>
  260. #define TOLOWER(c) (tolower(c))
  261. #else
  262. #define TOLOWER(c) (isupper(c) ? tolower(c) : (c))
  263. #endif
  264.  
  265. Local XtCallbackRec cb[2];
  266. #define XtSetCbk(argarray, rtn, arg) \
  267.      cb[0].callback = rtn; \
  268.      cb[0].closure = (caddr_t)arg; \
  269.      XtSetArg(argarray, XtNcallback, cb)
  270.  
  271. /*****************************************************************************
  272.  * Widget manipulation routines.                                             *
  273.  *****************************************************************************/
  274.  
  275. Local Boolean SetValues(current, request, new)
  276. Widget current, request, new;
  277. {
  278.      InfoWidget cw = (InfoWidget)current;
  279.      InfoWidget nw = (InfoWidget)new;
  280.      
  281.      if (cw->info.file != nw->info.file
  282.      || strcomp(cw->info.file, nw->info.file)
  283.      || cw->info.node != nw->info.node
  284.      || strcomp(cw->info.node, nw->info.node)) {
  285.       XtFree(cw->info.file);
  286.       XtFree(cw->info.node);
  287.       getNode(nw, nw->info.file, nw->info.node, NULL);
  288.      }
  289.      /* getNode() does the redisplay implicitly */
  290.      return FALSE;
  291. }
  292.  
  293. /* We only manage one widget (the pane) directly */
  294. Local XtGeometryResult GeometryManager(w, request, reply)
  295. InfoWidget w;
  296. XtWidgetGeometry *request;
  297. XtWidgetGeometry *reply; /* RETURN */
  298. {
  299.      XtGeometryResult res;
  300.      Dimension width, height;
  301.      
  302.      width = w->core.width;
  303.      height = w->core.height;
  304.      
  305.      /* We don't really care; see what daddy says */
  306.      res = XtMakeGeometryRequest(XtParent(w), request, reply);
  307.      if (res == XtGeometryNo)
  308.       return res;
  309.      else if (res == XtGeometryAlmost) {
  310.       if (reply->request_mode & CWWidth)
  311.            width = reply->width;
  312.       if (reply->request_mode & CWHeight)
  313.            height = reply->height;
  314.      }
  315.      else { /* Has to be XtGeometryYes */
  316.       if (request->request_mode & CWWidth)
  317.            width = request->width;
  318.       if (request->request_mode & CWHeight)
  319.            height = request->height;
  320.      }
  321.      XtResizeWidget(w, width, height, w->core.border_width);
  322. }
  323.  
  324. Local void Destroy(w)
  325. Widget w;
  326. {
  327.      InfoWidget iw = (InfoWidget)w;
  328.      
  329.      if (INDIRECT(iw).table)
  330.       FREE_TAG_TABLE(INDIRECT(iw));
  331.      if (TAGTABLE(iw).table)
  332.       FREE_TAG_TABLE(TAGTABLE(iw));
  333.      while (popNode(iw));    /* popNode will free all but last */
  334.      /* now free the last one */
  335.      if (CURNODE(iw)) {
  336.       XtFree(CURNODE(iw)->file);
  337.       XtFree(CURNODE(iw)->node);
  338.       FREE_LIST(CURNODE(iw)->menu);
  339.       FREE_LIST(CURNODE(iw)->xref);
  340.       XtFree(CURNODE(iw));
  341.      }
  342.      if (DATA(iw))
  343.       XtFree(DATA(iw));
  344.      XtFree(iw->info.file);
  345.      XtFree(iw->info.node);
  346. }
  347.  
  348. Local void Initialize(request, new)
  349. Widget request;
  350. Widget new;
  351. {
  352.      Arg args[15];
  353.      Cardinal i;
  354.      InfoWidget iw = (InfoWidget)new;
  355.      Widget top, box1, box2, vport, vport2;
  356.      char blanks[MAXSTR], *cp;
  357.      Import char *bzero();
  358.      
  359.      /* create a blank filled string as a placeholder for certain labels */
  360.      for (i = 0; i < MAXSTR - 1; i++)
  361.       blanks[i] = ' ';
  362.      blanks[i] = '\0';
  363.      
  364.      /* Pick some desperation defaults */
  365.      if (new->core.width == 0)
  366.       new->core.width = 100;
  367.      if (new->core.height == 0)
  368.       new->core.height = 50;
  369.      
  370.      /* Prevent later confusion */
  371.      iw->info.arg[0] = '\0';
  372.      
  373.      /* Create outer pane */
  374.      i = 0;
  375.      top = XtCreateManagedWidget("pane1", panedWidgetClass, new, args, i);
  376.      
  377.      /* Create top row of "main control" buttons and labels. */
  378.      i = 0;
  379.      box1 = XtCreateManagedWidget("box1", boxWidgetClass, top, args, i);
  380.      
  381.      if (iw->info.callback) {
  382.       Widget q;
  383.       
  384.       i = 0;
  385.       q = XtCreateManagedWidget("quit", commandWidgetClass,
  386.                     box1, args, i);
  387.       XtAddCallback(q, XtNcallback, do_quit, iw);
  388.      }
  389.      i = 0;
  390.      XtSetArg(args[i], XtNlabel, "File: ");                i++;
  391.      iw->info.fileLabel = XtCreateManagedWidget("file", labelWidgetClass,
  392.                         box1, args, i);
  393.      i = 0;
  394.      XtSetArg(args[i], XtNlabel, "Node: ");                i++;
  395.      iw->info.nodeLabel = XtCreateManagedWidget("node", labelWidgetClass,
  396.                         box1, args, i);
  397.      i = 0;
  398.      XtSetArg(args[i], XtNlabel, "Prev: ");                i++;
  399.      XtSetCbk(args[i], do_prev, iw);                    i++;
  400.      iw->info.prevCmd = XtCreateManagedWidget("prev", commandWidgetClass,
  401.                           box1, args, i);
  402.      i = 0;
  403.      XtSetArg(args[i], XtNlabel, "Up: ");                i++;
  404.      XtSetCbk(args[i], do_up, iw);                    i++;
  405.      iw->info.upCmd = XtCreateManagedWidget("up", commandWidgetClass,
  406.                         box1, args, i);
  407.      i = 0;
  408.      XtSetArg(args[i], XtNlabel, "Next: ");                i++;
  409.      XtSetCbk(args[i], do_next, iw);                    i++;
  410.      iw->info.nextCmd = XtCreateManagedWidget("next", commandWidgetClass,
  411.                           box1, args, i);
  412.      
  413.      /* Create the menu pane */
  414.      i = 0;
  415.      XtSetArg(args[i], XtNallowVert, TRUE);                i++;
  416.      vport = XtCreateManagedWidget("vport1", viewportWidgetClass,
  417.                    top, args, i);
  418.      
  419.      i = 0;
  420.      XtSetCbk(args[i], do_menu_sel, iw);                i++;
  421.      iw->info.menuList = XtCreateManagedWidget("menu", listWidgetClass,
  422.                            vport, args, i);
  423.      /*
  424.       * Create the text area for displaying node contents.
  425.       */
  426.      i = 0;
  427.      XtSetArg(args[i], XtNstring, blanks);                i++;
  428.      XtSetArg(args[i], XtNlength, MAXSTR);                i++;
  429.      XtSetArg(args[i], XtNeditType, XawtextRead);            i++;
  430.      XtSetArg(args[i], XtNuseStringInPlace, TRUE);            i++;
  431.      XtSetArg(args[i], XtNtype, XawAsciiString);            i++;
  432.      iw->info.nodeText = XtCreateManagedWidget("nodeText",
  433.                            asciiTextWidgetClass,
  434.                            top, args, i);
  435.      i = 0;
  436.      XtSetArg(args[i], XtNallowVert, TRUE);                i++;
  437.      vport2 = XtCreateManagedWidget("vport2", viewportWidgetClass,
  438.                     top, args, i);
  439.      
  440.      /* Create the xref pane */
  441.      i = 0;
  442.      XtSetCbk(args[i], do_xref_sel, iw);                i++;
  443.      iw->info.xrefList = XtCreateManagedWidget("xref", listWidgetClass,
  444.                            vport2, args, i);
  445.      
  446.      /*
  447.       * Create the bottom "auxilliary" command button group.
  448.       */
  449.      i = 0;
  450.      box2 = XtCreateManagedWidget("box2", boxWidgetClass,
  451.                   top, args, i);
  452.      i = 0;
  453.      XtSetCbk(args[i], do_menu, iw);                    i++;
  454.      iw->info.xrefCmd = XtCreateManagedWidget("menu", commandWidgetClass,
  455.                           box2, args, i);
  456.      i = 0;
  457.      XtSetCbk(args[i], do_xref, iw);                    i++;
  458.      iw->info.xrefCmd = XtCreateManagedWidget("xref", commandWidgetClass,
  459.                           box2, args, i);
  460.      i = 0;
  461.      XtSetCbk(args[i], do_goto, iw);                    i++;
  462.      iw->info.gotoCmd = XtCreateManagedWidget("goto", commandWidgetClass,
  463.                           box2, args, i);
  464.      i = 0;
  465.      XtSetCbk(args[i], do_search, iw);                    i++;
  466.      iw->info.searchCmd = XtCreateManagedWidget("search", commandWidgetClass,
  467.                         box2, args, i);
  468.      i = 0;
  469.      bzero(iw->info.arg, ARGLEN);
  470.      XtSetArg(args[i], XtNstring, iw->info.arg);            i++;
  471.      XtSetArg(args[i], XtNlength, ARGLEN);                i++;
  472.      XtSetArg(args[i], XtNuseStringInPlace, TRUE);            i++;
  473.      XtSetArg(args[i], XtNeditType, XawtextEdit);            i++;
  474.      iw->info.argText = XtCreateManagedWidget("arg", asciiTextWidgetClass,
  475.                           box2, args, i);
  476.      
  477.      /*
  478.       * Create the status and message area labels.
  479.       */
  480.      i = 0;
  481.      XtSetArg(args[i], XtNresize, FALSE);                i++;
  482.      XtSetArg(args[i], XtNlabel, blanks);                i++;
  483.      XtSetArg(args[i], XtNborderWidth, 0);                i++;
  484.      iw->info.statusLabel = XtCreateManagedWidget("status", labelWidgetClass,
  485.                           top, args, i);
  486.      i = 0;
  487.      XtSetArg(args[i], XtNresize, FALSE);                i++;
  488.      XtSetArg(args[i], XtNlabel, blanks);                i++;
  489.      XtSetArg(args[i], XtNborderWidth, 0);                i++;
  490.      iw->info.messageLabel = XtCreateManagedWidget("message", labelWidgetClass,
  491.                            top, args, i);
  492.      
  493.      /* set the initial node information */
  494.      ZERO_TABLE(INDIRECT(iw));
  495.      ZERO_TABLE(TAGTABLE(iw));
  496.      DATA(iw) = NULL;
  497.      CURNODE(iw) = NULL;
  498.      
  499.      iw->info.file = XtNewString(iw->info.file);
  500.      iw->info.node = XtNewString(iw->info.node);
  501.      
  502.      if (getNode(iw, iw->info.file, iw->info.node, NULL) == FALSE)
  503.       message(iw, "?Can't find initial file/node.");
  504. }
  505.  
  506. Local void Realize(w, value_mask, attributes)
  507. InfoWidget w;
  508. Mask *value_mask;
  509. XSetWindowAttributes *attributes;
  510. {
  511.      if (w->composite.num_children < 1)
  512.       XtError("No children?!?");
  513.      else {
  514.       /* Create window with which to manage child */
  515.       XtCreateWindow(w, (unsigned int)InputOutput,
  516.              (Visual *)CopyFromParent, *value_mask, attributes);
  517.       XtResizeWidget(w->composite.children[0], w->core.width,
  518.              w->core.height, 0);
  519.       /*
  520.        * Install accelerators onto widgets we know will need them.
  521.        * Note that Volume 4 of the O'Reilly "X Toolkit Intrinsics
  522.        * Programming Manual" (page 204, paragraph 5) says that widgets
  523.        * should never do this. I disagree: here's a case in point.
  524.        */
  525.       XtInstallAllAccelerators(w, w);
  526.       XtInstallAccelerators(w->info.nodeText, w);
  527.      }
  528. }
  529.  
  530. Local void Resize(w)
  531. InfoWidget w;
  532. {
  533.      XtResizeWidget(w->composite.children[0], w->core.width, w->core.height,
  534.             0);
  535. }
  536.  
  537. /*****************************************************************************
  538.  * Info file manipulation routines.                                          *
  539.  *****************************************************************************/
  540.  
  541. /* Here is the main guy. Handles all navigation within the info tree. */
  542. Local Boolean getNode(iw, file, node, pushTo)
  543. InfoWidget iw;
  544. String file, node;
  545. NodeInfo *pushTo;
  546. {
  547.      NodeInfo *cur;
  548.      int offset;
  549.      Boolean status = FALSE, needfile;
  550.      
  551.      if (node && index(node, '(') && index(node, ')')) {
  552.       file = substr(node, iindex(node, '(') + 1,
  553.             iindex(node, ')') - 1);
  554.       node = index(node, ')') + 1;
  555.      }
  556.      if (!node || !*node)
  557.       node = "Top";
  558.      
  559.      if (!file) {
  560.       file = iw->info.file;
  561.       needfile = !DATA(iw);
  562.      }
  563.      else
  564.       needfile = !DATA(iw) ||
  565.            strcomp(file_name(file), file_name(iw->info.file));
  566.      if (needfile) {
  567.       /* get a new file */
  568.       if ((file = getFile(iw, file, FALSE)) != NULL) {
  569.            if (file && iw->info.file != file) {
  570.             XtFree(iw->info.file);
  571.             iw->info.file = XtNewString(file);
  572.            }
  573.            iw->info.subFile = NULL;
  574.       }
  575.      }
  576.      else if (!strcomp(node, iw->info.node))
  577.       return TRUE;    /* we're already there */
  578.      else {
  579.       XtFree(iw->info.node);
  580.       iw->info.node = XtNewString(node);
  581.      }
  582.      if (file && (offset = findNode(iw, node)) >= 0) {
  583.       if (!pushTo) {
  584.            cur = pushNode(iw, iw->info.file, iw->info.node, offset);
  585.            parseNode(iw, cur, offset);
  586.       }
  587.       else
  588.            cur = pushTo;
  589.       displayNode(iw, cur);
  590.       message(iw, NULL);
  591.       showStatus(iw, cur);
  592.       if (!iw->info.retain_arg)
  593.            clear_arg(iw);
  594.       status = TRUE;
  595.      }
  596.      else {
  597.       /* Failed to get the new node, go back (but only once) */
  598.       if (!pushTo && CURNODE(iw))
  599.            getNode(iw, CURNODE(iw)->file, CURNODE(iw)->node, CURNODE(iw));
  600.      }
  601.      return status;
  602. }
  603.  
  604. #ifdef    UNCOMPRESS
  605.  
  606. Local Inline int iscompressed(fname)
  607. String fname;
  608. {
  609.      int len = strlen(fname);
  610.      
  611.      return len >= 2 && !strcmp(".Z", fname + len - 2);
  612. }
  613.  
  614. Local FILE* iopen(fname, dir)
  615. String fname;
  616. String dir;
  617. {
  618.      int len = strlen(fname);
  619.      
  620.      if (iscompressed(fname)) {
  621.       /* This is a compressed file. */
  622.       char cmd[MAXPATHLEN + sizeof(UNCOMPRESS) + 1];
  623.       
  624.       sprintf(cmd, "%s %s", UNCOMPRESS, fname);
  625.       return popen(cmd, dir);
  626.      }
  627.      else
  628.       return fopen(fname, dir);
  629. }
  630.  
  631. #else    /* !UNCOMPRESS */
  632.  
  633. #define iscompressed(fname)    (0)
  634. #define iopen(fname, dir)    fopen(fname, dir)
  635.  
  636. #endif    /* UNCOMPRESS */
  637.  
  638. /* Loads in file "name" and tag/indirect info, if any. */
  639. Local String getFile(iw, name, subfilep)
  640. InfoWidget iw;
  641. String name;
  642. Boolean subfilep;
  643. {
  644.      String ret;
  645.      
  646.      FILE *fp;
  647.      
  648.      ret = find_file(iw->info.path, name);
  649.      if (ret) {
  650.       Import int stat();
  651.       struct stat sb;
  652.  
  653.       if (!stat(ret, &sb) && (fp = iopen(ret, "r"))) {
  654.            int expected, total;
  655.            Boolean compressed = iscompressed(ret);
  656.  
  657.            if (DATA(iw)) {
  658.             XtFree(DATA(iw));
  659.             DATA(iw) = NULL;
  660.            }
  661.            /*
  662.         * If the file is compressed, we make a worst-case guess
  663.         * (though space-wise it's certainly the BEST case) that 
  664.         * the uncompressed data will be 3 times larger than the
  665.         * original file. This is REALLY KLUDGE but there's really
  666.         * not much else to do, short of uncompressing to a temp file
  667.         * which is even less reliable (what if you haven't got enough
  668.         * tmp space?). Oh well. We do realloc it later, once we know
  669.         * how much was really read.
  670.         */
  671.            expected = compressed ? 3 * sb.st_size : sb.st_size;
  672.            DATA(iw) = XtMalloc(expected + 1);
  673.            if (compressed) {
  674.             int left;
  675.             char *next;
  676.             
  677.             next = DATA(iw);
  678.             left = expected;
  679.             while (left >= 0) {
  680.              int nread;
  681.              extern int errno;
  682.             
  683.              errno = 0;
  684.              nread = fread(next, 1, left, fp);
  685.              if (nread > 0) {
  686.                   next += nread;
  687.                   left -= nread;
  688.              }
  689.              else if (errno) {
  690.                   message(iw, "?Read error on pipe for %s.", name);
  691.                   pclose(fp);
  692.                   fp = NULL;
  693.                   XtFree(DATA(iw));
  694.                   ret = NULL;
  695.                   break;
  696.              }
  697.              else {
  698.                   pclose(fp);
  699.                   fp = NULL;
  700.                   total = next - DATA(iw);
  701.                   break;
  702.              }
  703.             }
  704.             if (fp) {
  705.              message(iw, "?Underestimated expansion of %s.", ret);
  706.              pclose(fp);
  707.              fp = NULL;
  708.              XtFree(DATA(iw));
  709.              ret = NULL;
  710.             }
  711.             else /* reclaim excess space */
  712.              DATA(iw) = XtRealloc(DATA(iw), total + 1);
  713.            }
  714.            else {
  715.             /* Not compressed, just slurp it in */
  716.             if (fread(DATA(iw), 1, sb.st_size, fp) == sb.st_size) {
  717.              fclose(fp);
  718.              total = sb.st_size;
  719.             }
  720.             else {
  721.              message(iw, "?Read error on %s.", name);
  722.              fclose(fp);
  723.              XtFree(DATA(iw));
  724.              ret = NULL;
  725.             }
  726.            }
  727.            if (ret) {
  728.             DATA(iw)[DATASIZE(iw) = total] = '\0';
  729.             if (!subfilep) {
  730.              Boolean needIndirect;
  731.              needIndirect = parseTags(iw);
  732.              parseIndirect(iw, needIndirect);
  733.             }
  734.            }
  735.       }
  736.       else
  737.            ret = NULL;
  738.      }
  739.      return ret;
  740. }
  741.  
  742. /* Look through tag table (and/or current buffer) for a node */
  743. Local int findNode(iw, name)
  744. InfoWidget iw;
  745. String name;
  746. {
  747.      ID_P i;
  748.      int offset = -1;
  749.      String s, srch;
  750.      
  751.      /* A node name of "*" means the whole file */
  752.      if (!strcomp(name, "*"))
  753.       return 0;
  754.      
  755.      if (TAGTABLE(iw).table) {
  756.       for (i = TAGTABLE(iw).table; I_NAME(*i); i++) {
  757.            if (!strcomp(I_NAME(*i), name)) {
  758.             offset = I_OFFSET(*i);
  759.             break;
  760.            }
  761.       }
  762.       /* if we found the tag and there's an indirect table, adjust */
  763.       if (offset > 0 && INDIRECT(iw).table) {
  764.            String sub;
  765.            
  766.            for (i = INDIRECT(iw).table; I_NAME(*i); i++) {
  767.             if (I_OFFSET(*i) > offset)    /* got it */
  768.              break;
  769.            }
  770.            sub = I_NAME(*(--i));
  771.            if (strcomp(sub, iw->info.subFile)) {
  772.             if (!getFile(iw, sub, TRUE))
  773.              return 0;
  774.             else
  775.              iw->info.subFile = sub;
  776.            }
  777.            offset -= I_OFFSET(*i);
  778.            /* compensate for header */
  779.            offset += HDRSIZE(iw);
  780.       }
  781.      }
  782.      /*
  783.       * Now search forward for the node name. Note that this will
  784.       * work whether or not we found the tag in the tag table. Having
  785.       * found the tag only insures that we search a little less.
  786.       */
  787.      s = START(iw);
  788.      if (offset > 0)
  789.       s += offset;
  790.      
  791.      /*
  792.       * since bogus tags can leave us *after* the node start as well as
  793.       * before it, we risk a little extra searching and back up to the
  794.       * closest node marker above. Es tut mir leid, but this is what you
  795.       * get with out-of-date tags!
  796.       */
  797.      while (s > START(iw) && !INFO_CHAR(*s))
  798.       --s;
  799.      srch = strconcat(NODE_TOKEN, name);
  800.      while (s) {
  801.       if ((s = search(iw, s, END(iw), srch, TRUE)) != NULL) {
  802.            /* If not an exact match, keep looking */
  803.            if (!index(NAME, *s))
  804.             continue;
  805.            offset = INTOFF(START(iw), s);
  806.            /* found it, move to the beginning */ 
  807.            while(!INFO_CHAR(START(iw)[offset - 1]))
  808.             offset--;
  809.            s = NULL;
  810.       }
  811.       else
  812.            offset = -1;
  813.      }
  814.      return offset;
  815. }
  816.  
  817. /* Push a node onto the history list */
  818. Local NodeInfo *pushNode(iw, file, node, offset)
  819. InfoWidget iw;
  820. String file, node;
  821. int offset;
  822. {
  823.      NodeInfo *tmp;
  824.      
  825.      tmp = XtNew(NodeInfo);
  826.      bzero(tmp, sizeof(NodeInfo));
  827.      tmp->file = XtNewString(file);
  828.      tmp->node = XtNewString(node);
  829.      tmp->start = offset;
  830.      tmp->nextNode = CURNODE(iw);
  831.      CURNODE(iw) = tmp;
  832.      return tmp;
  833. }
  834.  
  835. /* Pop a node off the history list */
  836. Local NodeInfo *popNode(iw)
  837. InfoWidget iw;
  838. {
  839.      NodeInfo *tmp = NULL;
  840.      
  841.      if (CURNODE(iw) && CURNODE(iw)->nextNode) {
  842.       tmp = CURNODE(iw)->nextNode;
  843.       XtFree(CURNODE(iw)->file);
  844.       XtFree(CURNODE(iw)->node);
  845.       FREE_LIST(CURNODE(iw)->menu);
  846.       FREE_LIST(CURNODE(iw)->xref);
  847.       XtFree(CURNODE(iw));
  848.       CURNODE(iw) = tmp;
  849.      }
  850.      return tmp;
  851. }
  852.  
  853. /* Parse out all the header/menu/xref information for a node. */
  854. Local void parseNode(iw, n, offset)
  855. InfoWidget iw;
  856. NodeInfo *n;
  857. int offset;
  858. {
  859.      register String start = START(iw) + offset;
  860.      
  861.      /* was the whole file ("*") selected? */
  862.      if (offset == 0) {
  863.       n->length = DATASIZE(iw);
  864.       I_START(n->name) = I_LEN(n->name) = 0;
  865.       I_START(n->prev) = I_LEN(n->prev) = 0;
  866.       I_START(n->up) = I_LEN(n->up) = 0;
  867.       I_START(n->next) = I_LEN(n->next) = 0;
  868.       I_START(n->text) = 0;
  869.       I_LEN(n->text) = n->length;
  870.      }
  871.      else {
  872.       /* find the end of the node */
  873.       n->length = 0;
  874.       start = START(iw) + offset;
  875.       while (start < END(iw) && !INFO_CHAR(*start)) {
  876.            n->length++;
  877.            start++;
  878.       }
  879.      }
  880.      /* get the header */
  881.      parseHeader(iw, n);
  882.      /* get the menu items */
  883.      parseMenu(iw, n);
  884.      /* get the cross reference entries */
  885.      parseXRefs(iw, n);
  886. }
  887.  
  888. Local void parseHeader(iw, n)
  889. InfoWidget iw;
  890. NodeInfo *n;
  891. {
  892.      String strpbrk(), tmp;
  893.      
  894.      /* first, get the node name offset */
  895.      I_START(n->name) = INTOFF(START(iw), NSEARCH(iw, n, NODE_TOKEN));
  896.      I_LEN(n->name) = INTOFF(START(iw), strpbrk(START(iw) + I_START(n->name),
  897.                         NAME_END_TOKEN)) -
  898.                              I_START(n->name);
  899.      
  900.      /* now the prev, if any */
  901.      if ((I_START(n->prev) = INTOFF(START(iw),
  902.                     NSEARCH(iw, n, PREV_TOKEN))) > 0)
  903.       I_LEN(n->prev) = INTOFF(START(iw),
  904.                   strpbrk(START(iw) + I_START(n->prev),
  905.                       NAME_END_TOKEN)) -
  906.                            I_START(n->prev);
  907.      else
  908.       I_LEN(n->prev) = I_START(n->prev) = 0;
  909.      
  910.      /* and the up, if any */
  911.      if ((I_START(n->up) = INTOFF(START(iw),
  912.                   NSEARCH(iw, n, UP_TOKEN))) > 0)
  913.       I_LEN(n->up) = INTOFF(START(iw), strpbrk(START(iw) + I_START(n->up),
  914.                            NAME_END_TOKEN)) -
  915.                             I_START(n->up);
  916.      else
  917.       I_LEN(n->up) = I_START(n->up) = 0;
  918.      
  919.      /* the next, if any */
  920.      if ((I_START(n->next) = INTOFF(START(iw),
  921.                     NSEARCH(iw, n, NEXT_TOKEN))) > 0)
  922.       I_LEN(n->next) = INTOFF(START(iw),
  923.                   strpbrk(START(iw) + I_START(n->next),
  924.                       NAME_END_TOKEN)) -
  925.                            I_START(n->next);
  926.      else
  927.       I_LEN(n->next) = I_START(n->next) = 0;
  928.      
  929.      /* And finally skip over the header and set the text offset there */
  930.      tmp = START(iw) + I_START(n->name);
  931.      while (*tmp != '\n')
  932.       tmp++;
  933.      I_START(n->text) = INTOFF(START(iw), tmp + 1);
  934.      I_LEN(n->text) = n->length - (I_START(n->text) - n->start);
  935. }
  936.  
  937. Local void parseMenu(iw, n)
  938. InfoWidget iw;
  939. NodeInfo *n;
  940. {
  941.      register String mstart;
  942.      String strpbrk();
  943.      
  944.      /* start clean */
  945.      FREE_LIST(n->menu);
  946.      
  947.      /* Does node have a menu? */
  948.      if ((mstart = NSEARCH(iw, n, MENU_TOKEN)) != NULL) {
  949.       /* Initialize string list */
  950.       ALLOC_LIST(n->menu);
  951.       
  952.       /* go looking for menu items */
  953.       while (mstart = search(iw, mstart, NEND(iw, n), MENU_SEP_TOKEN,
  954.                  FALSE)) {
  955.            MAYBE_BUMP_LIST(n->menu);
  956.            /* put an offset entry in the table */
  957.            I_LEN(TPOS(n->menu.t)) = 0;
  958.            I_START(TPOS(n->menu.t)) = INTOFF(START(iw), mstart);
  959.            while (*(mstart++) != ':')
  960.             I_LEN(TPOS(n->menu.t))++;
  961.            /* now save the menu name as a string in the list */
  962.            LPOS(n->menu) = XtMalloc(I_LEN(TPOS(n->menu.t)) + 1);
  963.            strncpy(LPOS(n->menu), START(iw) + I_START(TPOS(n->menu.t)),
  964.                I_LEN(TPOS(n->menu.t)));
  965.            LPOS(n->menu)[I_LEN(TPOS(n->menu.t))] = '\0';
  966.            normalize_whitespace(LPOS(n->menu));
  967.            /* Is the menu name not the node name? */
  968.            if (*mstart != ':') {
  969.             int plev = 0;
  970.             
  971.             mstart = eat_whitespace(mstart);
  972.             I_START(TPOS(n->menu.t)) = INTOFF(START(iw), mstart);
  973.             while (*mstart != '\0' && !(plev == 0 &&
  974.                         index(NAME_END_TOKEN,
  975.                               *mstart) != NULL)) {
  976.              if (*mstart == '(')
  977.                   ++plev;
  978.              else if (*mstart == ')')
  979.                   --plev;
  980.              mstart++;
  981.              }
  982.             I_LEN(TPOS(n->menu.t)) =
  983.              INTOFF(START(iw), mstart) - I_START(TPOS(n->menu.t));
  984.            }
  985.            INCP(n->menu.t);
  986.       }
  987.       ROUND_LIST(n->menu);
  988.      }
  989. }
  990.  
  991. Local void parseXRefs(iw, n)
  992. InfoWidget iw;
  993. NodeInfo *n;
  994. {
  995.      register String nstart;
  996.      String strpbrk();
  997.      
  998.      /* start clean */
  999.      FREE_LIST(n->xref);
  1000.      
  1001.      /* Do we have any cross-reference entries? */
  1002.      if ((nstart = search(iw, NSTART(iw, n), NEND(iw, n), NOTE_TOKEN, TRUE))
  1003.      != NULL) {
  1004.       ALLOC_LIST(n->xref);
  1005.       nstart = NSTART(iw, n);
  1006.       
  1007.       /*
  1008.        * Go looking for cross-references (including the one we just
  1009.        * found; wasteful, but avoiding it would make for grotty code).
  1010.         */
  1011.       while (nstart = search(iw, nstart, NEND(iw, n), NOTE_TOKEN, TRUE)) {
  1012.            /* skip over whitespace */
  1013.            nstart = eat_whitespace(nstart);
  1014.            MAYBE_BUMP_LIST(n->xref);
  1015.            I_LEN(TPOS(n->xref.t)) = 0;
  1016.            I_START(TPOS(n->xref.t)) = INTOFF(START(iw), nstart);
  1017.            while (*(nstart++) != ':')
  1018.             I_LEN(TPOS(n->xref.t))++;
  1019.            /* save the note name as a string */
  1020.            LPOS(n->xref) = XtMalloc(I_LEN(TPOS(n->xref.t)) + 1);
  1021.            strncpy(LPOS(n->xref), START(iw) + I_START(TPOS(n->xref.t)),
  1022.                I_LEN(TPOS(n->xref.t)));
  1023.            LPOS(n->xref)[I_LEN(TPOS(n->xref.t))] = '\0';
  1024.            normalize_whitespace(LPOS(n->xref));
  1025.            /* Is the note name not the first part? */
  1026.            if (*nstart != ':') {
  1027.             nstart = eat_whitespace(nstart + 1);
  1028.             I_START(TPOS(n->xref.t)) = INTOFF(START(iw), nstart);
  1029.             I_LEN(TPOS(n->xref.t)) =
  1030.              INTOFF(START(iw), strpbrk(nstart, NAME_END_TOKEN)) -
  1031.                   I_START(TPOS(n->xref.t));
  1032.            }
  1033.            INCP(n->xref.t);
  1034.       }
  1035.       ROUND_LIST(n->xref);
  1036.      }
  1037. }           
  1038.  
  1039. /* Put the node information on the screen */
  1040. Local void displayNode(iw, n)
  1041. InfoWidget iw;
  1042. NodeInfo *n;
  1043. {
  1044.      Arg args[5];
  1045.      Cardinal i, lst_size;
  1046.      String *lst;
  1047.      Local char *nolist[] = { "", NULL };     /* make the list widget happy */
  1048.      Local char tmpfile[256];
  1049.      
  1050.      /* Make sure it doesn't try anything cute until we're ready */
  1051.      XawTextDisableRedisplay(iw->info.nodeText);
  1052.      
  1053.      /*
  1054.       * There exists a strange bug in the text widget that causes text
  1055.       * to be erroneously selected when we're mousing selections. Since
  1056.       * we don't want to keep things selected while we're navigating
  1057.       * anyway, this is a satisfactory workaround.
  1058.       */
  1059.      XawTextUnsetSelection(iw->info.nodeText);
  1060.      
  1061.      /* show the header */
  1062.      displayHeader(iw, n);
  1063.      
  1064.      /* show the menu */
  1065.      if (!n->menu.l) {
  1066.       lst = nolist;
  1067.       lst_size = 1;
  1068.      }
  1069.      else {
  1070.       lst = n->menu.l;
  1071.       lst_size = IDX(n->menu.t);
  1072.      }
  1073.      XawListChange(iw->info.menuList, lst, lst_size, 0, TRUE);
  1074.      
  1075.      /* change the xref list */
  1076.      if (!n->xref.l) {
  1077.       lst = nolist;
  1078.       lst_size = 1;
  1079.      }
  1080.      else {
  1081.       lst = n->xref.l;
  1082.       lst_size = IDX(n->xref.t);
  1083.      }
  1084.      XawListChange(iw->info.xrefList, lst, lst_size, 0, TRUE);
  1085.      
  1086.      /* Show the new text */
  1087.      i = 0;
  1088.      if (I_START(n->text)) {
  1089.       char *addr = (START(iw) + I_START(n->text) + I_LEN(n->text));
  1090.       
  1091.       XtSetArg(args[i], XtNstring, START(iw) + I_START(n->text));    i++;
  1092.       XtSetArg(args[i], XtNlength, I_LEN(n->text));            i++;
  1093.       if (INFO_CHAR(*addr))
  1094.            *addr = '\0';
  1095.       else {
  1096.            char msg[256];
  1097.            
  1098.            sprintf(msg, "Encountered bad terminator (%d) for node '%s'",
  1099.                *addr, n->name);
  1100.            XtWarning(msg);
  1101.       }
  1102.      }
  1103.      else {
  1104.       XtSetArg(args[i], XtNstring, START(iw));            i++;
  1105.       XtSetArg(args[i], XtNlength, DATASIZE(iw));            i++;
  1106.      }
  1107.      XtSetValues(iw->info.nodeText, args, i);
  1108.      
  1109.      /* Go for redisplay */
  1110.      XawTextEnableRedisplay(iw->info.nodeText);
  1111.      
  1112.      /* Stick the insertion marker at the top where it's out of the way */
  1113.      XawTextSetInsertionPoint(iw->info.nodeText, 0);
  1114. }
  1115.  
  1116. /* display the header information */
  1117. Local void displayHeader(iw, n)
  1118. InfoWidget iw;
  1119. NodeInfo *n;
  1120. {
  1121.      Arg args[5];
  1122.      Cardinal i;
  1123.      String tmp;
  1124.      int sensitive;
  1125.      
  1126.      /* set the file name */
  1127.      tmp = strconcat("File: ", file_name(iw->info.file));
  1128.      i = 0;
  1129.      XtSetArg(args[i], XtNlabel, tmp);                    i++;
  1130.      XtSetValues(iw->info.fileLabel, args, i);
  1131.      
  1132.      /* set the node name */
  1133.      i = 0;
  1134.      if ((tmp = offsetToString(iw, n->name)) != NULL)
  1135.       sensitive = TRUE;
  1136.      else
  1137.       sensitive = FALSE;
  1138.      XtSetArg(args[i], XtNlabel, strconcat("Node: ", tmp));        i++;
  1139.      XtSetArg(args[i], XtNsensitive, sensitive);            i++;
  1140.      XtSetValues(iw->info.nodeLabel, args, i);
  1141.      
  1142.      /* set the prev */
  1143.      i = 0;
  1144.      if ((tmp = offsetToString(iw, n->prev)) != NULL)
  1145.       sensitive = TRUE;
  1146.      else
  1147.       sensitive = FALSE;
  1148.      XtSetArg(args[i], XtNlabel, strconcat("Prev: ", tmp));        i++;
  1149.      XtSetArg(args[i], XtNsensitive, sensitive);            i++;
  1150.      XtSetValues(iw->info.prevCmd, args, i);
  1151.      
  1152.      /* set the up */
  1153.      i = 0;
  1154.      if ((tmp = offsetToString(iw, n->up)) != NULL)
  1155.       sensitive = TRUE;
  1156.      else
  1157.       sensitive = FALSE;
  1158.      XtSetArg(args[i], XtNlabel, strconcat("Up: ", tmp));        i++;
  1159.      XtSetArg(args[i], XtNsensitive, sensitive);            i++;
  1160.      XtSetValues(iw->info.upCmd, args, i);
  1161.      
  1162.      /* set the next */
  1163.      i = 0;
  1164.      if ((tmp = offsetToString(iw, n->next)) != NULL)
  1165.       sensitive = TRUE;
  1166.      else
  1167.       sensitive = FALSE;
  1168.      XtSetArg(args[i], XtNlabel, strconcat("Next: ", tmp));        i++;
  1169.      XtSetArg(args[i], XtNsensitive, sensitive);            i++;
  1170.      XtSetValues(iw->info.nextCmd, args, i);
  1171. }
  1172.  
  1173. /*
  1174.  * Look for tag table information in the current buffer. If tag table
  1175.  * is indirect, return TRUE, else return false.
  1176.  */
  1177. Local Boolean parseTags(iw)
  1178. InfoWidget iw;
  1179. {
  1180.      String start, s1;
  1181.      char tmp[MAXSTR];
  1182.      Boolean indirect = FALSE;
  1183.      int i;
  1184.      
  1185.      /*
  1186.       * go back about 8 lines. I don't know if this will always back up
  1187.       * past the end marker, but Emacs info seems to think so.
  1188.       */
  1189.      start = END(iw);
  1190.      i = 0;
  1191.      while (i < 8)
  1192.       if (*(--start) == '\n')
  1193.            i++;
  1194.      
  1195.      start = search(iw, start, END(iw), TAGEND_TOKEN, TRUE);
  1196.      if (start && (start = search_back(iw, start, START(iw),
  1197.                        TAGTABLE_TOKEN, TRUE))) {
  1198.       ALLOC_TABLE(TAGTABLE(iw));
  1199.       /* we were searching backward so move over the token */
  1200.       start += strlen(TAGTABLE_TOKEN);
  1201.       if ((s1 = search(iw, start, start + strlen(ITAGTABLE_TOKEN) + 10,
  1202.                ITAGTABLE_TOKEN, TRUE)) != NULL) {
  1203.            indirect = TRUE;
  1204.            start = s1;
  1205.       }
  1206.       while ((start = search(iw, start, END(iw), NODE_TOKEN, FALSE))
  1207.          != NULL) {
  1208.            MAYBE_BUMP_TABLE(TAGTABLE(iw));
  1209.            strccpy(tmp, start, DEL_CHAR);
  1210.            I_NAME(TPOS(TAGTABLE(iw))) = XtNewString(tmp);
  1211.            start += strlen(tmp) + 1;
  1212.            sscanf(start, "%d", &I_OFFSET(TPOS(TAGTABLE(iw))));
  1213.            INCP(TAGTABLE(iw));
  1214.       }
  1215.       ROUND_TABLE(TAGTABLE(iw));
  1216.      }
  1217.      else if (TAGTABLE(iw).table)
  1218.       FREE_TAG_TABLE(TAGTABLE(iw));
  1219.      return indirect;
  1220. }
  1221.  
  1222. /* Look for indirect file information in the current buffer */
  1223. Local void parseIndirect(iw, needIndirect)
  1224. InfoWidget iw;
  1225. Boolean needIndirect;
  1226. {
  1227.      String start;
  1228.      char tmp[MAXSTR], *s1;
  1229.      
  1230.      if (start = search(iw, START(iw), END(iw), INDIRECT_TOKEN, TRUE)) {
  1231.       /* move backwards looking for the INFO_CHAR */
  1232.       for (s1 = start; s1 >= START(iw) && !INFO_CHAR(*s1); s1--);
  1233.       if (s1 < START(iw)) {
  1234.            message(iw, "?Invalid indirect table for %s!", iw->info.file);
  1235.            return;
  1236.       }
  1237.       else
  1238.            HDRSIZE(iw) = INTOFF(START(iw), s1);
  1239.       ALLOC_TABLE(INDIRECT(iw));
  1240.       for (IDX(INDIRECT(iw)) = 0; !INFO_CHAR(*start); INCP(INDIRECT(iw))){
  1241.            MAYBE_BUMP_TABLE(INDIRECT(iw));
  1242.            strccpy(tmp, start, ':');
  1243.            I_NAME(TPOS(INDIRECT(iw))) = XtNewString(tmp);
  1244.            start += strlen(tmp) + 1;
  1245.            sscanf(start, "%d", &I_OFFSET(TPOS(INDIRECT(iw))));
  1246.            start = index(start, '\n') + 1;
  1247.       }
  1248.       ROUND_TABLE(INDIRECT(iw));
  1249.      }
  1250.      else if (needIndirect)
  1251.       message(iw, "?Indirect table not found for %s! Hilfe!",
  1252.           iw->info.file);
  1253.      else if (INDIRECT(iw).table)
  1254.       FREE_TAG_TABLE(INDIRECT(iw));
  1255. }
  1256.  
  1257. /*****************************************************************************
  1258.  * Text display functions.                                                   *
  1259.  *****************************************************************************/
  1260.  
  1261. /* display a message in the message area */
  1262. Local void message(iw, s, p1, p2, p3)
  1263. InfoWidget iw;
  1264. String s;
  1265. caddr_t p1, p2, p3;
  1266. {
  1267.      char msgbuf[MAXSTR];
  1268.      Arg args[5];
  1269.      Cardinal i;
  1270.      
  1271.      i = 0;
  1272.      if (s) {
  1273.       sprintf(msgbuf, s, p1, p2, p3);
  1274.       XtSetArg(args[i], XtNlabel, msgbuf);    i++;
  1275.       XtSetValues(iw->info.messageLabel, args, i);
  1276.       feep(iw);
  1277.       if (*s == '?')    /* a dire warning */
  1278.            XtWarning(msgbuf);
  1279.      }
  1280.      else {    /* clear the message area */
  1281.       XtSetArg(args[i], XtNlabel, " ");    i++;
  1282.       XtSetValues(iw->info.messageLabel, args, i);
  1283.      }
  1284. }
  1285.  
  1286. /* display the current node/file */
  1287. Local void showStatus(iw, n)
  1288. InfoWidget iw;
  1289. NodeInfo *n;
  1290. {
  1291.      char statbuf[MAXSTR];
  1292.      Arg args[5];
  1293.      Cardinal i;
  1294.      String sub = iw->info.subFile;
  1295.      
  1296.      sprintf(statbuf, "(%s)%s, %d characters%s", file_name(iw->info.file),
  1297.          iw->info.node, n->length,
  1298.          sub ? strconcat(", subfile: ", sub) : ".");
  1299.      i = 0;
  1300.      XtSetArg(args[i], XtNlabel, statbuf);    i++;
  1301.      XtSetValues(iw->info.statusLabel, args, i);
  1302. }
  1303.  
  1304. /*****************************************************************************
  1305.  * Functions used by actions                                                 *
  1306.  *****************************************************************************/
  1307.  
  1308. Local void Abort(w, event, params, num_params)
  1309. Widget   w;
  1310. XEvent   *event;
  1311. String   *params;
  1312. Cardinal *num_params;
  1313. {
  1314.      InfoWidget iw = find_top(w);
  1315.      
  1316.      feep(iw);
  1317.      do_dialog_abort(w, iw, NULL);
  1318. }
  1319.  
  1320. Local void Confirm(w, event, params, num_params)
  1321. Widget   w;
  1322. XEvent   *event;
  1323. String   *params;
  1324. Cardinal *num_params;
  1325. {
  1326.      InfoWidget iw = find_top(w);
  1327.      
  1328.      if (w == iw->info.argText)
  1329.       (*(iw->info.requester))(w, iw, NULL);
  1330.      else
  1331.       do_dialog_confirm(w, iw, NULL);
  1332. }
  1333.  
  1334. Local void NodeDir(w, event, params, num_params)
  1335. Widget   w;
  1336. XEvent   *event;
  1337. String   *params;
  1338. Cardinal *num_params;
  1339. {
  1340.      InfoWidget iw = find_top(w);
  1341.      
  1342.      if (getNode(iw, "dir", "Top", NULL) == FALSE)
  1343.       message(iw, "?Yow! The directory seems to have disappeared!\n");
  1344. }
  1345.  
  1346. Local void NodeNext(w, event, params, num_params)
  1347. Widget   w;
  1348. XEvent   *event;
  1349. String   *params;
  1350. Cardinal *num_params;
  1351. {
  1352.      do_next(NULL, find_top(w), NULL);
  1353. }
  1354.  
  1355. Local void NodePrev(w, event, params, num_params)
  1356. Widget   w;
  1357. XEvent   *event;
  1358. String   *params;
  1359. Cardinal *num_params;
  1360. {
  1361.      do_prev(NULL, find_top(w), NULL);
  1362. }
  1363.  
  1364.  
  1365. Local void NodeUp(w, event, params, num_params)
  1366. Widget   w;
  1367. XEvent   *event;
  1368. String   *params;
  1369. Cardinal *num_params;
  1370. {
  1371.      do_up(NULL, find_top(w), NULL);
  1372. }
  1373.  
  1374. Local void NodeTop(w, event, params, num_params)
  1375. Widget   w;
  1376. XEvent   *event;
  1377. String   *params;
  1378. Cardinal *num_params;
  1379. {
  1380.      InfoWidget iw = find_top(w);
  1381.      
  1382.      if (getNode(iw, NULL, "Top", NULL) == FALSE)
  1383.       message(iw, "?This node has no top! Bad joss!");
  1384. }
  1385.  
  1386. Local void NodeLast(w, event, params, num_params)
  1387. Widget   w;
  1388. XEvent   *event;
  1389. String   *params;
  1390. Cardinal *num_params;
  1391. {
  1392.      NodeInfo *tmp;
  1393.      InfoWidget iw = find_top(w);
  1394.      
  1395.      if ((tmp = popNode(iw)) != NULL) {
  1396.       if (getNode(iw, tmp->file, tmp->node, tmp) == FALSE)
  1397.            message(iw, "?Can't pop back to node (%s)%s! We're hosed!",
  1398.                tmp->file, tmp->node);
  1399.      }
  1400.      else
  1401.       message(iw, "No further history.");
  1402. }
  1403.  
  1404. Local void NodeXRef(w, event, params, num_params)
  1405. Widget   w;
  1406. XEvent   *event;
  1407. String   *params;
  1408. Cardinal *num_params;
  1409. {
  1410.      do_xref(NULL, find_top(w), NULL);
  1411. }
  1412.  
  1413. Local void NodeGoto(w, event, params, num_params)
  1414. Widget   w;
  1415. XEvent   *event;
  1416. String   *params;
  1417. Cardinal *num_params;
  1418. {
  1419.      do_goto(NULL, find_top(w), NULL);
  1420. }
  1421.  
  1422. Local void NodeSearch(w, event, params, num_params)
  1423. Widget   w;
  1424. XEvent   *event;
  1425. String   *params;
  1426. Cardinal *num_params;
  1427. {
  1428.      do_search(NULL, find_top(w), NULL);
  1429. }
  1430.  
  1431. Local void NodeQuit(w, event, params, num_params)
  1432. Widget   w;
  1433. XEvent   *event;
  1434. String   *params;
  1435. Cardinal *num_params;
  1436. {
  1437.      do_quit(NULL, find_top(w), NULL);
  1438. }
  1439.  
  1440. Local void NodeTutorial(w, event, params, num_params)
  1441. Widget   w;
  1442. XEvent   *event;
  1443. String   *params;
  1444. Cardinal *num_params;
  1445. {
  1446.      InfoWidget iw = find_top(w);
  1447.      
  1448.      if (getNode(iw, "info", "Help", NULL) == FALSE)
  1449.       message(iw, "?Hmmm. I can't seem to find the info tutorial!");
  1450. }
  1451.  
  1452. Local void NodeHelp(w, event, params, num_params)
  1453. Widget   w;
  1454. XEvent   *event;
  1455. String   *params;
  1456. Cardinal *num_params;
  1457. {
  1458.      Cardinal i;
  1459.      Arg args[10];
  1460.      InfoWidget iw = find_top(w);
  1461.      
  1462.      if (!iw->info.helpPopup) {
  1463.       Widget hpane, htext;
  1464.       Local XtCallbackRec cb[2];
  1465.       
  1466.       /* create the help popup */
  1467.       i = 0;
  1468.       iw->info.helpPopup = XtCreatePopupShell("help",
  1469.                           transientShellWidgetClass,
  1470.                           iw, args, i);
  1471.       i = 0;
  1472.       hpane = XtCreateManagedWidget("pane", panedWidgetClass,
  1473.                     iw->info.helpPopup, args, i);
  1474.       i = 0;
  1475.       cb[0].callback = do_popdown;
  1476.       cb[0].closure = (caddr_t)iw->info.helpPopup;
  1477.       XtSetArg(args[i], XtNcallback, cb);            i++;
  1478.       XtCreateManagedWidget("Close", commandWidgetClass,
  1479.                 hpane, args, i);
  1480.       i = 0;
  1481.       XtSetArg(args[i], XtNtype, XawAsciiString);        i++;
  1482.       XtSetArg(args[i], XtNeditType, XawtextRead);        i++;
  1483.       htext = XtCreateManagedWidget("text", asciiTextWidgetClass,
  1484.                     hpane, args, i);
  1485.      }
  1486.      
  1487.      i = 0;
  1488.      XtSetArg(args[i], XtNx, event->xbutton.x);            i++;
  1489.      XtSetArg(args[i], XtNy, event->xbutton.y);            i++;
  1490.      XtSetValues(iw->info.helpPopup, args, i);
  1491.      
  1492.      XtPopup(iw->info.helpPopup, XtGrabNonexclusive);
  1493. }
  1494.  
  1495. Local void ButtonSelection(w, event, params, num_params)
  1496. Widget   w;
  1497. XEvent   *event;
  1498. String   *params;
  1499. Cardinal *num_params;
  1500. {
  1501.      char tmp[MAXTOKEN], *idx;
  1502.      XawTextPosition beg, end, nlen;
  1503.      XawTextBlock ret, asterisk, colon;
  1504.      InfoWidget iw = find_top(w);
  1505.      
  1506.      SET_BLOCK(asterisk, 0, 1, "*");
  1507.      SET_BLOCK(colon, 0, 1, ":");
  1508.      
  1509.      /* Next, try and get a complete "item" selected */
  1510.      if ((beg = XawTextSearch(w, XawsdLeft, &asterisk)) != XawTextSearchError)
  1511.       XawTextSetInsertionPoint(w, beg);
  1512.      else
  1513.       return;    /* Bomb out */
  1514.      if ((end = XawTextSearch(w, XawsdRight, &colon)) != XawTextSearchError &&
  1515.      end > beg) {
  1516.       long len = end - beg;
  1517.       
  1518.       /* Victory! Now try and figure out what it is */
  1519.       if (!XawTextSourceRead(XawTextGetSource(w), beg, &ret, len))
  1520.            return;    /* If can't read, forget it */
  1521.       else while (ret.length && *(ret.ptr) == '*' || isspace(*(ret.ptr)))
  1522.            --ret.length, ++ret.ptr;
  1523.       if (!ret.length || ret.length >= MAXTOKEN) {
  1524.            feep(iw);
  1525.            return;    /* Nothing left, forget it */
  1526.           }
  1527.       else {
  1528.            strncpy(tmp, ret.ptr, ret.length);
  1529.            tmp[ret.length] = '\0';
  1530.            normalize_whitespace(tmp);
  1531.            if (!strncomp(tmp, "note ", 5)) {
  1532.             if (!(idx = trueName(iw, CURNODE(iw)->xref, tmp + 5)))
  1533.              feep(iw);
  1534.             else if (getNode(iw, NULL, idx, NULL) == FALSE)
  1535.              message(iw, "?Can't find cross reference for '%s'!",
  1536.                  idx);
  1537.            }
  1538.            else {
  1539.             if (!(idx = trueName(iw, CURNODE(iw)->menu, tmp)))
  1540.              feep(iw);
  1541.             else if (getNode(iw, NULL, idx, NULL) == FALSE)
  1542.              message(iw, "?Can't find menu entry for '%s'!",
  1543.                  idx);
  1544.            }
  1545.       }            
  1546.      }
  1547. }
  1548.  
  1549. Local void NodeMenuSelectByNumber(w, event, params, num_params)
  1550. Widget   w;
  1551. XEvent   *event;
  1552. String   *params;
  1553. Cardinal *num_params;
  1554. {
  1555.      Import int atoi();
  1556.      int menunum;
  1557.      int nitems;
  1558.      InfoWidget iw = find_top(w);
  1559.      
  1560.      nitems = IDX(CURNODE(iw)->menu.t);
  1561.      menunum = atoi(*params);
  1562.      /* menu number of zero means get menu from arg area */
  1563.      if (!menunum)
  1564.       do_menu(NULL, iw, NULL);
  1565.      else if (!nitems)
  1566.       message(iw, "No menu for this node.");
  1567.      else if (menunum > nitems)
  1568.       message(iw, "There are only %d menu items.", nitems);
  1569.      else {
  1570.       XawListHighlight(iw->info.menuList, menunum - 1);
  1571.       if (getNode(iw, NULL,
  1572.               offsetToString(iw,CURNODE(iw)->menu.t.table[menunum-1]),
  1573.               NULL) == FALSE)
  1574.            message(iw, "?Can't find node for menu item #%s", *params);
  1575.      }
  1576. }
  1577.  
  1578. Local void NodePrint(w, event, params, num_params)
  1579. Widget   w;
  1580. XEvent   *event;
  1581. String   *params;
  1582. Cardinal *num_params;
  1583. {
  1584.      Import int unlink();
  1585.      String tmp;
  1586.      FILE *out;
  1587.      InfoWidget iw = find_top(w);
  1588.      
  1589.      /* if you don't have this routine in your stdlib, make one up */
  1590.      tmp = tmpnam(NULL);
  1591.      
  1592.      if (!CURNODE(iw))
  1593.       message(iw, "?No current node?");
  1594.      else if ((out = fopen(tmp, "w")) == NULL)
  1595.       message(iw, "?Can't open temporary file '%s'.", tmp);
  1596.      else {
  1597.       String s1 = NSTART(iw, CURNODE(iw));
  1598.       String s2 = NEND(iw, CURNODE(iw));
  1599.       char syscmd[MAXSTR];
  1600.       int stat;
  1601.       
  1602.       fwrite(s1, s2 - s1, 1, out);
  1603.       fclose(out);
  1604.       
  1605.       message(iw, "Sending '%s' to the printer, please wait..",
  1606.           iw->info.node);
  1607.       
  1608.       sprintf(syscmd, "%s %s", iw->info.printCmd, tmp);
  1609.       if ((stat = system(syscmd)) != 0)
  1610.            message(iw, "?'%s' failed with exit status %d. Help!",
  1611.                syscmd, stat);
  1612.       else
  1613.            message(iw, "Finished printing.");
  1614.       unlink(tmp);
  1615.      }
  1616.      
  1617.      
  1618. }
  1619.  
  1620. /*****************************************************************************
  1621.  * Functions used from callback lists.                                       *
  1622.  *****************************************************************************/
  1623.  
  1624. /* Abort the dialog operation */
  1625. Local void do_dialog_abort(w, client_data, call_data)
  1626. Widget w;
  1627. caddr_t client_data;
  1628. caddr_t call_data;
  1629. {
  1630.      InfoWidget iw = (InfoWidget)client_data;
  1631.      
  1632.      if (w == iw->info.argText)
  1633.       clear_arg(iw);
  1634.      else
  1635.       XtDestroyWidget(iw->info.argPopup);
  1636. }
  1637.  
  1638. /* Confirm the dialog operation */
  1639. Local void do_dialog_confirm(w, client_data, call_data)
  1640. Widget w;
  1641. caddr_t client_data;
  1642. caddr_t call_data;
  1643. {
  1644.      InfoWidget iw = (InfoWidget)client_data;
  1645.      XawTextBlock blk;
  1646.      
  1647.      XtDestroyWidget(iw->info.argPopup);
  1648.      
  1649.      SET_BLOCK(blk, 0, 0, NULL);
  1650.      if ((blk.ptr = XawDialogGetValueString(XtParent(w))) &&
  1651.      (blk.length = strlen(blk.ptr))) {
  1652.       if (blk.length > ARGLEN)    /* truncate if necessary */
  1653.            blk.ptr[blk.length = ARGLEN] = '\0';
  1654.       XawTextReplace(iw->info.argText, 0, blk.length, &blk);
  1655.       (*(iw->info.requester))(w, iw, NULL);
  1656.      }
  1657. }
  1658.  
  1659. /*
  1660.  * Seems there should be a better way of doing this. Methinks the
  1661.  * XtCallbackPopdown() stuff isn't general enough. Should be a way of
  1662.  * doing this (and only this).
  1663.  */
  1664. Local void do_popdown(w, client_data, call_data)
  1665. Widget w;
  1666. caddr_t client_data;
  1667. caddr_t call_data;
  1668. {
  1669.      XtPopdown((Widget)client_data);
  1670. }
  1671.  
  1672. Local void do_prev(w, client_data, call_data)
  1673. Widget w;
  1674. caddr_t client_data;
  1675. caddr_t call_data;
  1676. {
  1677.      InfoWidget iw = (InfoWidget)client_data;
  1678.      String tmp;
  1679.      
  1680.      if ((tmp = offsetToString(iw, CURNODE(iw)->prev))) {
  1681.       if (getNode(iw, NULL, tmp, NULL) == FALSE)
  1682.            message(iw, "?Can't find the previous (%s) for this node.",
  1683.                tmp);
  1684.      }
  1685.      else
  1686.       message(iw, "Node has no previous");
  1687. }
  1688.  
  1689. Local void do_quit(w, client_data, call_data)
  1690. Widget w;
  1691. caddr_t client_data;
  1692. caddr_t call_data;
  1693. {
  1694.      InfoWidget iw = (InfoWidget)client_data;
  1695.      
  1696.      if (XtHasCallbacks(iw, XtNcallback) != XtCallbackHasSome)
  1697.       message(iw, "Sorry, I just don't know how to quit.");
  1698.      else
  1699.       XtCallCallbacks(iw, XtNcallback, NULL);
  1700. }
  1701.  
  1702. Local void do_up(w, client_data, call_data)
  1703. Widget w;
  1704. caddr_t client_data;
  1705. caddr_t call_data;
  1706. {
  1707.      InfoWidget iw = (InfoWidget)client_data;
  1708.      String tmp;
  1709.      
  1710.      if ((tmp = offsetToString(iw, CURNODE(iw)->up))) {
  1711.       if (getNode(iw, NULL, tmp, NULL) == FALSE)
  1712.            message(iw, "?Can't find the up (%s) for this node.", tmp);
  1713.      }
  1714.      else
  1715.       message(iw, "Node has no up");
  1716. }
  1717.  
  1718. Local void do_next(w, client_data, call_data)
  1719. Widget w;
  1720. caddr_t client_data;
  1721. caddr_t call_data;
  1722. {
  1723.      InfoWidget iw = (InfoWidget)client_data;
  1724.      String tmp;
  1725.      
  1726.      if ((tmp = offsetToString(iw, CURNODE(iw)->next))) {
  1727.       if (getNode(iw, NULL, tmp, NULL) == FALSE)
  1728.            message(iw, "?Can't find the next (%s) for this node.", tmp);
  1729.      }
  1730.      else
  1731.       message(iw, "Node has no next");
  1732. }
  1733.  
  1734. Local void do_xref(w, client_data, call_data)
  1735. Widget w;
  1736. caddr_t client_data;
  1737. caddr_t call_data;
  1738. {
  1739.      InfoWidget iw = (InfoWidget)client_data;
  1740.      String tmp;
  1741.      
  1742.      if ((tmp = get_arg(iw)) != NULL) {
  1743.       if ((tmp = trueName(iw, CURNODE(iw)->xref, tmp)) == NULL)
  1744.            message(iw, "No cross reference entry named '%s' in this node.",
  1745.                get_arg(iw));
  1746.       else if (getNode(iw, NULL, tmp, NULL) == FALSE)
  1747.            message(iw, "?Can't find node for xref item '%s'!",
  1748.                get_arg(iw));
  1749.      }
  1750.      else
  1751.       dialog(iw, "Please specify a cross reference:", do_xref);
  1752. }
  1753.  
  1754. Local void do_menu(w, client_data, call_data)
  1755. Widget w;
  1756. caddr_t client_data;
  1757. caddr_t call_data;
  1758. {
  1759.      InfoWidget iw = (InfoWidget)client_data;
  1760.      String tmp;
  1761.      
  1762.      if ((tmp = get_arg(iw)) != NULL) {
  1763.       if ((tmp = trueName(iw, CURNODE(iw)->menu, tmp)) == NULL)
  1764.            message(iw, "No menu entry named '%s' in this node.",
  1765.                get_arg(iw));
  1766.       else if (getNode(iw, NULL, tmp, NULL) == FALSE)
  1767.            message(iw, "?Can't find node for menu item '%s'",
  1768.                get_arg(iw));
  1769.      }
  1770.      else
  1771.       dialog(iw, "Please specify a menu entry:", do_menu);
  1772. }
  1773.  
  1774. Local void do_goto(w, client_data, call_data)
  1775. Widget w;
  1776. caddr_t client_data;
  1777. caddr_t call_data;
  1778. {
  1779.      InfoWidget iw = (InfoWidget)client_data;
  1780.      String tmp;
  1781.      
  1782.      if ((tmp = get_arg(iw)) != NULL) {
  1783.       char saveit[ARGLEN];
  1784.       
  1785.       /*
  1786.            * The string tmp points to will get nuked if getNode() fails
  1787.        * (as part of the cleanup process), so we need to save it if
  1788.        * we want to be able to print a meaningful error message.
  1789.        */
  1790.       strcpy(saveit, tmp);
  1791.       if (getNode(iw, NULL, tmp, NULL) == FALSE)
  1792.            message(iw, "Can't find a node named %s", saveit);
  1793.      }
  1794.      else
  1795.       dialog(iw, "Please specify the name of a node to goto:", do_goto);
  1796. }
  1797.  
  1798. /*
  1799.  * Implement a somewhat simplistic search strategy. If file has an indirect
  1800.  * list, look for a match in the tag table (since just looking in the current
  1801.  * file probably wouldn't be very useful). If not, then search the current
  1802.  * file. If we're successful in either case, record the position (in the
  1803.  * tags table or the file) so that we don't hit it again right away.
  1804.  */
  1805. Local void do_search(w, client_data, call_data)
  1806. Widget w;
  1807. caddr_t client_data;
  1808. caddr_t call_data;
  1809. {
  1810.      InfoWidget iw = (InfoWidget)client_data;
  1811.      String tmp, s;
  1812.      char name[MAXSTR];
  1813.      Local struct {
  1814.       String file;
  1815.       caddr_t pos;
  1816.      } oldPos;
  1817.      
  1818.      if ((tmp = get_arg(iw)) != NULL) {
  1819.       /* if remembered position is invalid, reset it */
  1820.       if (strcomp(oldPos.file, iw->info.file)) {
  1821.            oldPos.file = iw->info.file;
  1822.            oldPos.pos = NULL;
  1823.       }
  1824.       if (INDIRECT(iw).table) {
  1825.            ID_P i;
  1826.            int len = strlen(tmp);
  1827.            
  1828.            if (oldPos.pos)
  1829.             i = (ID_P)oldPos.pos;
  1830.            else
  1831.             i = TAGTABLE(iw).table;
  1832.            /* do a tags search */
  1833.            while (I_NAME(*i)) {
  1834.             if (!strncomp(I_NAME(*i), tmp, len))
  1835.              break;
  1836.             i++;
  1837.            }
  1838.            /* success? */
  1839.            if (I_NAME(*i)) {
  1840.             oldPos.pos = (caddr_t)(i + 1);
  1841.             if (getNode(iw, iw->info.file, I_NAME(*i), NULL) == FALSE)
  1842.              message(iw, "?Can't find node for tag %s!",
  1843.                  I_NAME(*i));
  1844.            }
  1845.            else {
  1846.             message(iw, "Tag search for '%s' failed.", tmp);
  1847.             oldPos.pos = NULL;
  1848.            }
  1849.       }
  1850.       else {
  1851.            if (oldPos.pos)
  1852.             s = (String)oldPos.pos;
  1853.            else
  1854.             s = START(iw);
  1855.            if ((s = search(iw, s, END(iw), 
  1856.                    strconcat(NODE_TOKEN, tmp),
  1857.                    TRUE)) != NULL) {
  1858.             int i;
  1859.             
  1860.             oldPos.pos = (caddr_t)s;
  1861.             strcpy(name, tmp);
  1862.             i = strlen(name);
  1863.             while (!index(NAME, *s))
  1864.              name[i++] = *s++;
  1865.             name[i] = '\0';
  1866.             if (getNode(iw, iw->info.file, name, NULL) == FALSE)
  1867.              message(iw, "?Can't find node name in search!");
  1868.            }
  1869.            else {
  1870.             message(iw, "Search for '%s' failed.", tmp);
  1871.             oldPos.pos = NULL;
  1872.            }
  1873.       }
  1874.      }
  1875.      else
  1876.       dialog(iw, "Please enter a string to search for:", do_search);
  1877. }
  1878.  
  1879. /* These two handle selections from the menu and xref lists */
  1880.  
  1881. Local void do_menu_sel(w, client_data, call_data)
  1882. Widget w;
  1883. caddr_t client_data;
  1884. caddr_t call_data;
  1885. {
  1886.      InfoWidget iw = (InfoWidget)client_data;
  1887.      XawListReturnStruct *rs = (XawListReturnStruct *)call_data;
  1888.      
  1889.      if (getNode(iw, NULL, trueName(iw, CURNODE(iw)->menu, rs->string),
  1890.          NULL) == FALSE)
  1891.       message(iw, "?Can't find node for menu item '%s'", rs->string);
  1892. }
  1893.  
  1894. Local void do_xref_sel(w, client_data, call_data)
  1895. Widget w;
  1896. caddr_t client_data;
  1897. caddr_t call_data;
  1898. {
  1899.      InfoWidget iw = (InfoWidget)client_data;
  1900.      XawListReturnStruct *rs = (XawListReturnStruct *)call_data;
  1901.      
  1902.      if (getNode(iw, NULL, trueName(iw, CURNODE(iw)->xref, rs->string),
  1903.          NULL) == FALSE)
  1904.       message(iw, "?Can't find node for cross reference '%s'", rs->string);
  1905. }
  1906.  
  1907. /*****************************************************************************
  1908.  * Xlib and toolkit utility functions.                                       *
  1909.  *****************************************************************************/
  1910.  
  1911. /* Clear the argument text */
  1912. Local void clear_arg(iw)
  1913. InfoWidget iw;
  1914. {
  1915.      XawTextBlock blk;
  1916.      
  1917.      SET_BLOCK(blk, 0, 0, "");
  1918.      XawTextReplace(iw->info.argText, 0, strlen(iw->info.arg), &blk);
  1919. }
  1920.  
  1921. /* Put up a dialog to get necessary information */
  1922. Local void dialog(iw, msg, callback)
  1923. InfoWidget iw;
  1924. String msg;
  1925. void (*callback)();
  1926. {
  1927.      Arg args[10];
  1928.      Cardinal i;
  1929.      int x, y;
  1930.      Widget dg, abort, confirm;
  1931.      
  1932.      /*
  1933.       * We create the dialog everytime (rather than just once, followed
  1934.       * by Popup/Popdown requests) so that it will be made the proper size
  1935.       * for the label each time. Can't seem to get it to resize dynamically,
  1936.       * so I don't see any other way.
  1937.       */
  1938.      iw->info.requester = callback;
  1939.      
  1940.      /* Don't see any other way of doing this. It seems there should be. */
  1941.      getXY(iw, &x, &y);
  1942.      
  1943.      /* Position nicely */
  1944.      i = 0;
  1945.      XtSetArg(args[i], XtNx, x - 30 > 0 ? x - 30 : 0);    i++;
  1946.      XtSetArg(args[i], XtNy, y - 30 > 0 ? y - 30 : 0);    i++;
  1947.      XtSetArg(args[i], XtNallowShellResize, TRUE);    i++;
  1948.      iw->info.argPopup = XtCreatePopupShell("need_argument",
  1949.                         transientShellWidgetClass,
  1950.                         iw, args, i);
  1951.      i = 0;
  1952.      XtSetArg(args[i], XtNvalue, iw->info.arg);        i++;
  1953.      XtSetArg(args[i], XtNlabel, msg);            i++;
  1954.      dg = XtCreateManagedWidget("dialog", dialogWidgetClass,
  1955.                 iw->info.argPopup, args, i);
  1956.      
  1957.      i = 0;
  1958.      abort = XtCreateManagedWidget("abort", commandWidgetClass,
  1959.                    dg, args, i);
  1960.      XtAddCallback(abort, XtNcallback, do_dialog_abort, iw);
  1961.      
  1962.      i = 0;
  1963.      confirm = XtCreateManagedWidget("confirm", commandWidgetClass,
  1964.                      dg, args, i);
  1965.      XtAddCallback(confirm, XtNcallback, do_dialog_confirm, iw);
  1966.      
  1967.      XtPopup(iw->info.argPopup, XtGrabExclusive);
  1968. }
  1969.  
  1970. /* Toot the horn */
  1971. Local void feep(iw)
  1972. InfoWidget iw;
  1973. {
  1974.      XBell(XtDisplay(iw), iw->info.bell_volume);
  1975. }
  1976.  
  1977. /* Find the info widget in a hierarchy */
  1978. Local Inline InfoWidget find_top(w)
  1979. Widget w;
  1980. {
  1981.      register Widget tmp = w;
  1982.      
  1983.      while (tmp) {
  1984.       if (XtClass(tmp) == infoWidgetClass)
  1985.            return (InfoWidget)tmp;
  1986.       else
  1987.            tmp = XtParent(tmp);
  1988.      }
  1989.      if (!tmp)
  1990.       XtError("Walked off end of widget hierarchy!");
  1991.      return (InfoWidget)NULL;
  1992. }
  1993.  
  1994. /* Return the arg contents if set, else NULL */
  1995. Local String get_arg(w)
  1996. InfoWidget w;
  1997. {
  1998.      if (strlen(w->info.arg))
  1999.       return w->info.arg;
  2000.      else
  2001.       return NULL;
  2002. }
  2003.  
  2004. /* Return the root XY coords of the pointer */
  2005. Local void getXY(w, xp, yp)
  2006. Widget w;
  2007. int *xp, *yp;
  2008. {
  2009.      Window junkr, junkc;
  2010.      int junkx, junky;
  2011.      unsigned int mask;
  2012.      
  2013.      (void) XQueryPointer(XtDisplay(w), XtWindow(w), &junkr, &junkc,
  2014.               xp, yp, &junkx, &junky, &mask);
  2015. }
  2016.  
  2017. /*****************************************************************************
  2018.  * Unix and string utility functions.                                        *
  2019.  *****************************************************************************/
  2020.  
  2021. /* Search for a file along a path, returning the complete path name if found */
  2022. Local String find_file(path, name)
  2023. String path, name;
  2024. {
  2025.      String cp = path;
  2026.      Boolean more_path = TRUE;
  2027.      Local char dir[MAXPATHLEN];
  2028.      int status = -1;
  2029.      String name_start, z_start;
  2030.      
  2031.      dir[0] = '\0';
  2032.      
  2033.      /* absolute path name? */
  2034.      if (name[0] == '/') {
  2035.       if (!access(name, R_OK))
  2036.            return name;
  2037.       else
  2038.            name = file_name(name);
  2039.      }
  2040.      while (status && more_path) {
  2041.           if ((cp = index(path, ':')) != NULL) {
  2042.                strncpy(dir, path, cp - path);
  2043.            dir[cp - path] = '\0';
  2044.                strcat(dir, "/");
  2045.                path = cp + 1;
  2046.           }
  2047.           else {
  2048.                strcpy(dir, path);
  2049.                strcat(dir, "/");
  2050.                more_path = FALSE;
  2051.           } 
  2052.       name_start = dir+strlen(dir);
  2053.       strcat(dir, name);
  2054.           status = access(dir, R_OK);
  2055. #ifdef UNCOMPRESS
  2056.       if (status) {
  2057.            z_start = dir+strlen(dir);
  2058.            strcat(dir,".Z");
  2059.            status = access(dir, R_OK);
  2060.       }
  2061. #endif
  2062.       /* if we failed, try again in lower case */
  2063.       if (status) {
  2064.            downcase(name_start);
  2065. #ifdef UNCOMPRESS
  2066.            *z_start = '\0';
  2067. #endif
  2068.            status = access(dir, R_OK);
  2069. #ifdef UNCOMPRESS
  2070.            if (status) {
  2071.             strcat(dir,".Z");
  2072.             status = access(dir, R_OK);
  2073.            }
  2074. #endif
  2075.       }
  2076.      }
  2077.      if (dir[0])
  2078.           return dir;
  2079.      else
  2080.           return NULL;
  2081. }
  2082.  
  2083. /* return the file part of a path name */
  2084. Local Inline String file_name(s)
  2085. register String s;
  2086. {
  2087.      register int i = strlen(s);
  2088.      
  2089.      while (i) {
  2090.       if (s[i - 1] == '/')
  2091.            return s + i;
  2092.       i--;
  2093.      }
  2094.      return s;
  2095. }
  2096.  
  2097. /* strip evil tab/formfeed/newline chars from a string (replacing w/blanks) */
  2098. Local Inline String normalize_whitespace(s)
  2099. String s;
  2100. {
  2101.      register String tmp;
  2102.      
  2103.      if (tmp = s) {
  2104.       while (*tmp) {
  2105.            if (isspace(*tmp))
  2106.             *tmp = ' ';
  2107.            ++tmp;
  2108.       }
  2109.      }
  2110.      return s;
  2111. }
  2112.  
  2113. /* Convert from an offset ID to a string. */
  2114. Local Inline String offsetToString(iw, blk)
  2115. InfoWidget iw;
  2116. ID blk;
  2117. {
  2118.      Local char ret[MAXSTR];
  2119.      
  2120.      if (I_LEN(blk) != 0) {
  2121.       strncpy(ret, START(iw) + I_START(blk), I_LEN(blk));
  2122.       ret[I_LEN(blk)] = '\0';
  2123.       return normalize_whitespace(ret);
  2124.      }
  2125.      else
  2126.       return NULL;
  2127. }
  2128.  
  2129. /* chew through white space */
  2130. Local Inline String eat_whitespace(s)
  2131. register String s;
  2132. {
  2133.      while (*s && isspace(*s))
  2134.       s++;
  2135.      return s;
  2136. }
  2137.  
  2138. /* look up the actual name of a list item */
  2139. Local String trueName(iw, lst, name)
  2140. InfoWidget iw;
  2141. IDList lst;
  2142. String name;
  2143. {
  2144.      register int i;
  2145.      
  2146.      for (i = 0; i < lst.t.idx; i++)
  2147.       if (!strcomp(lst.l[i], name))
  2148.            return offsetToString(iw, lst.t.table[i]);
  2149.      return NULL;
  2150. }
  2151.  
  2152. /* Search for a string */
  2153. Local String search(iw, start, end, str, igncase)
  2154. InfoWidget iw;
  2155. register String start, end, str;
  2156. Boolean igncase;
  2157. {
  2158.      register String ind = str;
  2159.      register String stop = str + strlen(str);
  2160.      register int comp;
  2161.      
  2162.      while (start < end) {
  2163.       if (!igncase)
  2164.            comp = (*start == *ind);
  2165.       else
  2166.            comp = (TOLOWER(*start) == TOLOWER(*ind));
  2167.       if (!comp) {
  2168.            if (ind != str)
  2169.             ind = str;
  2170.            else
  2171.             start++;
  2172.       }
  2173.       else {
  2174.            if (++start <= end && ++ind == stop)
  2175.             return start;
  2176.       }
  2177.      }
  2178.      return NULL;
  2179. }
  2180.  
  2181. /* Like search(), but in the reverse direction */
  2182. Local String search_back(iw, start, end, str, igncase)
  2183. InfoWidget iw;
  2184. register String start, end, str;
  2185. Boolean igncase;
  2186. {
  2187.      register String ind;
  2188.      register String stop;
  2189.      register int comp;
  2190.      
  2191.      ind = str = reverse(str);
  2192.      stop = ind + strlen(ind);
  2193.      
  2194.      while (start > end) {
  2195.       if (!igncase)
  2196.            comp = (*start == *ind);
  2197.       else
  2198.            comp = (TOLOWER(*start) == TOLOWER(*ind));
  2199.       if (!comp) {
  2200.            if (ind != str)
  2201.             ind = str;
  2202.            else
  2203.             start--;
  2204.       }
  2205.       else {
  2206.            start--;
  2207.            if (++ind == stop)
  2208.             return start;
  2209.       }
  2210.      }
  2211.      return NULL;
  2212. }
  2213.  
  2214. /*
  2215.  * Safe and sane strcmp. Deals with null pointer for either arg and ignores
  2216.  * case. All whitespace is considered equivalent.
  2217.  */
  2218. Local Inline int strcomp(s1, s2)
  2219. register String s1, s2;
  2220. {
  2221.      if (s1 && s2) {
  2222.       if (strlen(s1) != strlen(s2))
  2223.            return -1;
  2224.       
  2225.       while (*s1 && *s2 && (TOLOWER(*s1) == TOLOWER(*s2)))
  2226.            ++s1, ++s2;
  2227.       if (!*s1 && !*s2)
  2228.            return 0;
  2229.       else if (*s1 < *s2)
  2230.            return -1;
  2231.       else
  2232.            return 1;
  2233.      }
  2234.      else if (!s1 && !s2)
  2235.           return 0;
  2236.      else if (!s1 && s2)
  2237.           return -1;
  2238.      else
  2239.           return 1;
  2240. }
  2241.  
  2242. /* like above, but stops after n characters */
  2243. Local Inline int strncomp(s1, s2, n)
  2244. register String s1, s2;
  2245. int n;
  2246. {
  2247.      register String s3 = s2 + n;
  2248.      
  2249.      if (s1 && s2) {
  2250.       while (s2 < s3 && *s1 && *s2 && (TOLOWER(*s1) == TOLOWER(*s2)))
  2251.            ++s1, ++s2;
  2252.       if (!*s1 && !*s2 || s2 == s3)
  2253.            return 0;
  2254.       else if (*s1 < *s2)
  2255.            return -1;
  2256.       else
  2257.            return 1;
  2258.      }
  2259.      else if (!s1 && !s2)
  2260.           return 0;
  2261.      else if (!s1 && s2)
  2262.           return -1;
  2263.      else
  2264.           return 1;
  2265. }
  2266.  
  2267. /* Copy s2 to s1 up to (but not including) character c */
  2268. Local Inline void strccpy(s1, s2, c)
  2269. register String s1, s2;
  2270. register char c;
  2271. {
  2272.      while (*s2 && *s2 != c)
  2273.       *(s1++) = *(s2++);
  2274.      *s1 = '\0';
  2275. }
  2276.  
  2277. /*
  2278.  * Return integer subscript of character 'c' in string 's'.
  2279.  * (why doesn't this already exist in a library somewhere?).
  2280.  */
  2281. Local Inline int iindex(s, c)
  2282. register char *s, c;
  2283. {
  2284.      register char *cp;
  2285.      
  2286.      if (!s)
  2287.           return -1;
  2288.      cp = index(s, c);
  2289.      if (cp)
  2290.           return cp - s;
  2291.      else
  2292.           return -1;
  2293. }
  2294.  
  2295. Local String substr(s, p1, p2)
  2296. register String s;
  2297. register int p1, p2;
  2298. {
  2299.      Local char ret[MAXSTR];
  2300.      register int i = 0;
  2301.      
  2302.      if (p1 > p2) {
  2303.       sprintf(ret, "substr: start %d, end %d. start must be <= end",
  2304.           p1, p2);
  2305.       XtWarning(ret);
  2306.           return NULL;
  2307.      }
  2308.      if (p2 - p1 > MAXSTR) {
  2309.           sprintf(ret, "substr: end - start is > max len of %d", MAXSTR);
  2310.       XtWarning(ret);
  2311.           return NULL;
  2312.      }
  2313.      while (p1 <= p2)
  2314.           ret[i++] = s[p1++];
  2315.      ret[i] = '\0';
  2316.      return ret;
  2317. }
  2318.  
  2319. /*
  2320.  * Safely concatenate two strings into static area, returning pointer to
  2321.  * result.
  2322.  */
  2323. Local String strconcat(s1, s2)
  2324. register String s1, s2;
  2325. {
  2326.      Local char ret[MAXSTR];
  2327.      int len1;
  2328.      
  2329.      if (s1) {
  2330.       if ((len1 = strlen(s1)) >= MAXSTR) {
  2331.            sprintf(ret, "strconcat: length of s1 > MAX (%d)", MAXSTR);
  2332.            XtWarning(ret);
  2333.            return NULL;
  2334.       }
  2335.       else
  2336.            strcpy(ret, s1);
  2337.       if (s2) {
  2338.            if (len1 + strlen(s2) > MAXSTR) {
  2339.             sprintf(ret, "strconcat: length of s1 + s2 is > MAX (%d)",
  2340.                 MAXSTR);
  2341.             XtWarning(ret);
  2342.            }
  2343.            else
  2344.             strcat(ret, s2);
  2345.       }
  2346.       return ret;
  2347.      }
  2348.      else
  2349.       return NULL;
  2350. }
  2351.  
  2352. /* reverse a string so that a simple reverse search may be done on it */
  2353. Local String reverse(s)
  2354. register String s;
  2355. {
  2356.      Local char ret[MAXSTR];
  2357.      register int i, len;
  2358.      
  2359.      if ((len = strlen(s)) > MAXSTR) {
  2360.       sprintf(ret, "reverse: string too long to reverse. MAX is %d",
  2361.           MAXSTR);
  2362.       XtWarning(ret);
  2363.       return NULL;
  2364.      }
  2365.      else {
  2366.       i = 0;
  2367.       while (len)
  2368.            ret[i++] = s[--len];
  2369.       ret[i] = '\0';
  2370.       return ret;
  2371.      }
  2372. }
  2373.  
  2374. /* convert a string to lower case */
  2375. Local Inline String downcase(s)
  2376. register String s;
  2377. {
  2378.      String orig = s;
  2379.      
  2380.      if (s)
  2381.       while (*s) {
  2382.            *s = TOLOWER(*s);
  2383.            s++;
  2384.       }
  2385.      return orig;
  2386. }
  2387.  
  2388. #ifdef BSD
  2389. /* BSD users don't have strpbrk() */
  2390. /* Routines borrowed from PD libc written by Richard A. O'Keefe. */
  2391.  
  2392. #if     CharsAreSigned
  2393. #define MaxPosChar      127
  2394. #else  ~CharsAreSigned
  2395. #define MaxPosChar      255
  2396. #endif  CharsAreSigned
  2397. #ifndef _AlphabetSize
  2398. #define _AlphabetSize   128
  2399. #endif
  2400.  
  2401. static int  _set_ctr = MaxPosChar;
  2402. static char _set_vec[_AlphabetSize];
  2403.  
  2404. void _str2set(set)
  2405. register String set;
  2406. {
  2407.      if (set == NULL)
  2408.       return;
  2409.      if (++_set_ctr == MaxPosChar+1) {
  2410.       register char *w = &_set_vec[_AlphabetSize];
  2411.       do
  2412.            *--w = '\0';
  2413.       while (w != &_set_vec[0]);
  2414.           _set_ctr = 1;
  2415.      }
  2416.      while (*set)
  2417.       _set_vec[*set++] = _set_ctr;
  2418. }
  2419.  
  2420. String strpbrk(s1, s2)
  2421. register String s1, s2;
  2422. {
  2423.      _str2set(set);
  2424.      while (_set_vec[*str] != _set_ctr)
  2425.           if (!*str++)
  2426.            return NULL;
  2427.      return str;
  2428. }
  2429. #endif /* BSD */
  2430.